#include<iostream>
#include<cstdio>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
int dis[N][N];
int n, m, q;
int arr[N];
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i == j) {
				dis[i][j] = 0;
			}
			else {
				dis[i][j] = INF;
			}
		}
	}
	while (m--) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		dis[a][b] = dis[b][a] = min(dis[a][b], c);
	}
	scanf("%d", &q);
	int k = 0;
	while (q--) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		for (; arr[k] <= c && k < n; k++) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
				}
			}
		}
		if (dis[a][b] == INF || arr[a] > c || arr[b] > c) {
			printf("%d\n", -1);
		}
		else {
			printf("%d\n", dis[a][b]);
		}
	}
	return 0;
}